package com.example.icongyou.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.example.icongyou.R;
import com.example.icongyou.api.ChapterApi;
import com.example.icongyou.bean.Chapter;
import com.example.icongyou.retrofit.RetrofitClient;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ChapterDetailActivity extends AppCompatActivity {

    private TextView tvChapterTitle;   // 章节标题
    private TextView tvChapterContent; // 章节内容

    private int chapterId; // 从课程详情页传递的章节ID

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_chapter_detail);

        // 处理系统栏间距
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(android.R.id.content), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        // 获取传递的章节ID
        chapterId = getIntent().getIntExtra("chapterId", -1);
        if (chapterId == -1) {
            Toast.makeText(this, "章节ID无效", Toast.LENGTH_SHORT).show();
            finish();
            return;
        }

        // 初始化控件
        initViews();

        // 加载并展示章节详情
        loadAndShowChapterDetail();
    }

    /**
     * 初始化UI控件
     */
    private void initViews() {
        tvChapterTitle = findViewById(R.id.tv_chapter_title);
        tvChapterContent = findViewById(R.id.tv_chapter_content);
    }

    /**
     * 加载章节详情并展示
     */
    private void loadAndShowChapterDetail() {
        // 调用ChapterApi接口查询章节详情
        ChapterApi chapterApi = RetrofitClient.getChapterApi();
        chapterApi.getChapterById(chapterId).enqueue(new Callback<Chapter>() {
            @Override
            public void onResponse(Call<Chapter> call, Response<Chapter> response) {
                if (response.isSuccessful() && response.body() != null) {
                    // 接口调用成功，展示章节数据
                    Chapter chapter = response.body();
                    showChapterData(chapter);
                } else {
                    // 接口调用成功但无数据
                    Toast.makeText(ChapterDetailActivity.this, "获取章节详情失败", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<Chapter> call, Throwable t) {
                // 网络请求失败
                Toast.makeText(ChapterDetailActivity.this, "网络错误：" + t.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });
    }

    /**
     * 将章节数据展示到UI
     */
    private void showChapterData(Chapter chapter) {
        // 章节标题（使用传递的chapterTitle或接口返回的getTitle()）
        String title = getIntent().getStringExtra("chapterTitle");
        if (title == null || title.isEmpty()) {
            title = chapter.getTitle(); // 从接口数据中获取
        }
        tvChapterTitle.setText(title);

        // 章节内容（使用Chapter类的getContent()方法，与字段content匹配）
        String content = chapter.getContent();
        tvChapterContent.setText(content != null ? content : "该章节暂无内容");
    }

}